﻿using HungryCrowdLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HungryCrowd.server
{
    public partial class ajax : System.Web.UI.Page
    {
        public HungryCrowdLibrary.ModelDto.Trader Trader { get; set; }
        public List<HungryCrowdLibrary.ModelDto.Trader> Vendors { get; set; }
        public List<HungryCrowdLibrary.ModelDto.Market> Markets { get; set; }
        public string Result { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.Form["MethodName"])
            {
                case "GetTrader":
                    GetTrader();
                    break;
                case "GetListOfTraders":
                    GetListOfTraders();
                    break;
                case "GetListOfMarkets":
                    GetListOfMarkets();
                    break;
                case "SaveCategory":
                    SaveCategory();
                    break;
                case "SaveTrader":
                    SaveTrader();
                    break;
                case "SaveLocation":
                    SaveLocation();
                    break;
                case "SaveMarket":
                    SaveMarket();
                    break;
                case "GetTradersByCategory":
                    GetTradersByCategory();
                    break;
            }
            
            Response.Clear();
            Response.ContentType = "application/json";
            //Result = string.Empty;

            switch (Request.Form["MethodName"])
            {
                case "GetListOfTraders":
                    Result = new JavaScriptSerializer().Serialize(Vendors);
                    break;

                case "GetTrader":
                    Result = new JavaScriptSerializer().Serialize(Trader);
                    break;

                case "GetListOfMarkets":
                    Result = new JavaScriptSerializer().Serialize(Markets);
                    break;
                case "GetTradersByCategory":
                    Result = new JavaScriptSerializer().Serialize(Vendors);
                    break;
            }
            Response.Write(Result);
            Response.End();
        }

        private void GetTradersByCategory()
        {
            var categoryId = int.Parse(Request.Form["CategoryId"].ToString());
            var traders = API.TraderManager.GetList(x => x.CategoryId == categoryId && x.IsActive == true);
            var query = from v in traders
                        select new HungryCrowdLibrary.ModelDto.Trader
                        {
                            Id = v.Id,
                            Name = v.Name,
                            Description = v.Description,
                            ContactPerson = v.ContactPerson,
                            ContactNumber = v.ContactNumber,
                            EmailAddress = v.EmailAddress,
                            TradingDays = v.TradingDays,
                            TradingHours = v.TradingHours,
                            TradingArea = v.TradingArea,
                            TradingYears = v.TradingYears,
                            IsActive = v.IsActive,
                            MarketId = v.MarketId,
                        };

            Vendors = query.ToList();
        }

        public void GetTrader()
        {
            var traderId = int.Parse(Request.Form["traderId"].ToString());
            var traders = API.TraderManager.GetList(x => x.Id == traderId && x.IsActive == true);
            var query = from v in traders
                        select new HungryCrowdLibrary.ModelDto.Trader
                        {
                            Id = v.Id,
                            Name = v.Name,
                            Description = v.Description,
                            ContactPerson = v.ContactPerson,
                            ContactNumber = v.ContactNumber,
                            EmailAddress = v.EmailAddress,
                            TradingDays = v.TradingDays,
                            TradingHours = v.TradingHours,
                            TradingArea = v.TradingArea,
                            TradingYears = v.TradingYears,
                            IsActive = v.IsActive,
                            MarketId = v.MarketId,
                            ImageUrl = v.ImageURL
                        }; 
            Trader = query.SingleOrDefault();
        }

        public void GetListOfTraders()
        {
            var categoryId = string.IsNullOrWhiteSpace(Request.Form["categoryId"]) ? 0 : int.Parse(Request.Form["categoryId"]);
            var traders = API.TraderManager.GetList(x => x.IsActive && x.CategoryId == categoryId);
            var query = from v in traders
                        select new HungryCrowdLibrary.ModelDto.Trader
                        {
                            Id = v.Id,
                            Name = v.Name,
                            Description = v.Description,
                            ContactPerson = v.ContactPerson,
                            ContactNumber = v.ContactNumber,
                            EmailAddress = v.EmailAddress,
                            TradingDays = v.TradingDays,
                            TradingHours = v.TradingHours,
                            TradingArea = v.TradingArea,
                            TradingYears = v.TradingYears,
                            ImageUrl = v.ImageURL,
                            IsActive = v.IsActive,
                            MarketId = v.MarketId
                        };

            Vendors = query.ToList();
        }

        public void GetListOfMarkets()
        {
            var locationId = int.Parse(Request.Form["locationId"]);
            var markets = API.MarketManager.GetList(x => x.IsActive && x.LocationId == locationId);
            var marketList = from m in markets
                             select new HungryCrowdLibrary.ModelDto.Market
                             {
                                 Id = m.Id,
                                 Name = m.Name,
                                 IsActive = m.IsActive,
                                 LocationId = m.LocationId.Value
                             };
            Markets = marketList.ToList();
            //Markets = API.MarketManager.GetList(locationId);
        }

        public void SaveCategory()
        {
            var cat = Request.Form["Category"];
            var category = new JavaScriptSerializer().Deserialize(cat,typeof(Category));
            var success = API.CategoryManager.SaveChanges((Category)category);
            Result = success.ToString();
        }

        public void SaveTrader()
        {
            var traderObj = Request.Form["Trader"];
            var trader = new JavaScriptSerializer().Deserialize(traderObj, typeof(Trader));
            var traderDto = (Trader)trader;
            traderDto.CreateStampDate = DateTime.Now;
            var success = API.TraderManager.SaveChanges(traderDto);
            Result = success.ToString();
        }

        public void SaveLocation()
        {
            var locationObj = Request.Form["Location"];
            var location = new JavaScriptSerializer().Deserialize(locationObj, typeof(Location));
            var locationDto = (Location)location;
            locationDto.CreateStampDate = DateTime.Now;
            var success = API.LocationManager.SaveChanges(locationDto);
            Result = success.ToString();
        }

        public void SaveMarket()
        {
            var marketObj = Request.Form["Market"];
            var market = new JavaScriptSerializer().Deserialize(marketObj, typeof(Market));
            var marketDto = (Market)market;
            marketDto.CreateStampDate = DateTime.Now;
            var success = API.MarketManager.SaveChanges(marketDto);
            Result = success.ToString();
        }
    }
}